package com.example.home.fg

import android.util.Log
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.GsonUtils
import com.example.base.app.path.RouterPath
import com.example.home.adapter.NoticeAdapter
import com.example.common.view.view.BaseMvvmFragment
import com.example.home.R
import com.example.home.adapter.HomeAdapter
import com.example.home.databinding.FragmentHomeBinding
import com.example.home.entity.HomeListEntity
import com.example.home.vm.HomeViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class HomeFragment : BaseMvvmFragment<FragmentHomeBinding>() {
    override fun getLayoutId(): Int = R.layout.fragment_home
    val noticeAdapter = NoticeAdapter()
    private val homeViewModel:HomeViewModel by activityViewModels()
    private val homeListData = mutableListOf(
        HomeListEntity(1,com.example.resource.R.drawable.park_home,"车位管理",0),
        HomeListEntity(2,com.example.resource.R.drawable.visitor_home,"访客登记",1),
        HomeListEntity(3,com.example.resource.R.drawable.repair_home,"物业报修",2),
        HomeListEntity(4,com.example.resource.R.drawable.sign_home,"考勤打卡",3),
        HomeListEntity(5,com.example.resource.R.drawable.house_home,"房源信息",4),
        HomeListEntity(6,com.example.resource.R.drawable.shop_home,"商家推荐",5),
        HomeListEntity(7,com.example.resource.R.drawable.member_home,"月卡会员",6),
        HomeListEntity(8,com.example.resource.R.drawable.device_home,"设备管理",7)
    )

    override fun initView() {
        super.initView()
        mDataBinding.rvNotice.layoutManager = LinearLayoutManager(requireContext())
        mDataBinding.rvNotice.adapter = noticeAdapter
        //设置热门服务
        mDataBinding.rvHome.run {
            layoutManager = GridLayoutManager(this@HomeFragment.requireContext(),4)
            adapter = HomeAdapter(homeListData).apply {
                setItemOnClickListener(object :HomeAdapter.ItemOnClickListener{
                    override fun onClick(view: View, position: Int) {
                        this@HomeFragment.dispatchServicePage(homeListData[position])
                    }
                })
            }
        }
    }
    //分发服务页面
    private fun dispatchServicePage(homeList:HomeListEntity){
        var pagePath = when(homeList.id){
            0 -> RouterPath.PATH_PARK_PAGE      //车位管理
            1 -> RouterPath.PATH_VISITOR_PAGE   //访客登记
            2 -> RouterPath.PATH_REPAIR_PAGE    //物业报修
            3 -> RouterPath.PATH_PUNCH_PAGE     //考勤打卡
            4 -> RouterPath.PATH_HOUSE_PAGE     //房源信息
            5 -> RouterPath.PATH_MERCHANT_PAGE  //商家推荐
            6 -> RouterPath.PATH_VIP_PAGE       //月卡会员
            7 -> RouterPath.PATH_DEVICES_ORDER    //设备管理
            else -> RouterPath.PATH_EMPTY_PAGE
        }
        ARouter.getInstance().build(pagePath).navigation()
    }
    override fun loadData() {
        homeViewModel.loadNoticeData(3,10)
    }

    override fun observeDataChange() {
        homeViewModel.mSuccess.observe(requireActivity()){
            Log.e("HomePage","========>${GsonUtils.toJson(it)}")
            noticeAdapter.data.addAll(it)
            noticeAdapter.notifyDataSetChanged()
        }
    }
}